﻿Imports System
Imports System.Data
Imports System.Data.SqlClient


Public Class AyncCallbackExample
    Private m_connNorthwind As SqlConnection
    Private m_commandEmployees As SqlCommand

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionStringNorthwind As String = "server=localhost;async=true;uid=sa;pwd=thiru;database=northwind;"
        'Define two connection objects, one for each database
        m_connNorthwind = New SqlConnection(connectionStringNorthwind)
        'Open the connection
        m_connNorthwind.Open()
        'Start the execution of the sql query
        m_commandEmployees = New SqlCommand("Select Count(*) as EmployeesCount from Employees", m_connNorthwind)
        'Pass the name of the callback method as an argument
        Dim arEmployees As IAsyncResult = m_commandEmployees.BeginExecuteReader(AddressOf QueryCallbackMethod, Nothing)
    End Sub

    Sub QueryCallbackMethod(ByVal ar As IAsyncResult)
        Try
            Dim readerEmployees As SqlDataReader = m_commandEmployees.EndExecuteReader(ar)
            While (readerEmployees.Read())
                MessageBox.Show(readerEmployees("EmployeesCount"))
            End While
        Catch ex As Exception
            'Display the message if there is an exception
            MessageBox.Show(ex.Message)
        Finally
            m_connNorthwind.Close()
        End Try
    End Sub
End Class